package com.rankeiot.core.util;


import org.sagacity.sqltoy.SqlToyConstants;
import org.sagacity.sqltoy.utils.SnowflakeIdWorker;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Base64;

@SuppressWarnings("AlibabaConstantFieldShouldBeUpperCase")
public class IDUtil {
    static final SecureRandom numberGenerator = new SecureRandom();
    static SnowflakeIdWorker snowflakeIdWorker;
    public static String toString(long id) {
        return Long.toString(id, Character.MAX_RADIX);
    }
    private static byte[] getRandomBytes() {
        byte[] randomBytes = new byte[16];
        numberGenerator.nextBytes(randomBytes);
        randomBytes[6] &= 0x0f;  /* clear version        */
        randomBytes[6] |= 0x40;  /* set to version 4     */
        randomBytes[8] &= 0x3f;  /* clear variant        */
        randomBytes[8] |= 0x80;  /* set to IETF variant  */
        return randomBytes;
    }
    /**
     * java uuid 转换为36进制的短uuid,长度为24位
     *
     * @return
     */
    public static String getUUID() {
        return new BigInteger(1, getRandomBytes()).toString(Character.MAX_RADIX).toUpperCase();
    }
    /**
     * base64 UUID,长度为22位,生成速度更快,但不适合用于数据库id(有大小写和符号)
     * @return
     */
    public static String base64UUID() {
        return Base64.getUrlEncoder().withoutPadding().encodeToString(getRandomBytes());
    }
    /**
     *  获取雪花ID 19位
     */
    public static synchronized long getSnowflakeId(){
        if(snowflakeIdWorker==null){
            snowflakeIdWorker=new SnowflakeIdWorker(SqlToyConstants.WORKER_ID, SqlToyConstants.DATA_CENTER_ID);
        }
        return snowflakeIdWorker.nextId();
    }

    /**
     * 短雪花ID 12位
     * @return
     */
    public static String getSnowflakeIdStr(){
        return Long.toString(getSnowflakeId(),Character.MAX_RADIX).toUpperCase();
    }

    /**
     * 序号增长1
     * @param sn
     * @return
     */
    public static String increase(String sn) {
        if (StringUtil.isEmpty(sn)) {
            return sn;
        }
        int l = sn.length();
        long v = Long.parseLong(sn, Character.MAX_RADIX);
        String nv = Long.toString(v + 1, Character.MAX_RADIX).toUpperCase();
        return StringUtil.leftPad(nv, '0', l);
    }
}
